/***********************************************************************
* vw Name: dbo.vw_customer_futures_realized
* Created: 20100210, RGM
* Purpose: listado de los futuros comprados para la relacion cliente - producto
*	y que fueron dados de alta desde una recepcion de una instruccion de compra
*
* Modified: yyyymmdd User, data
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
ALTER VIEW [dbo].[vw_customer_futures_realized] AS
SELECT pif.application_future_period_id instruction_application_future_period_id, 
		cfwa.application_future_period_id future_application_future_period_id, 
		pid.customer_id, 
		piid.item_id, 
		SUM(cfwa.futures_weighted_avg) quantity
	FROM purchases_instructions_details pid, 
		 purchases_instructions_items_details piid,
		purchases_instructions_futures pif, realizeds_instructions ri, 
		vw_customer_futures_weighted_avg cfwa
	WHERE piid.purchase_instruction_detail_id = pid.purchase_instruction_detail_id
		AND pid.purchase_instruction_detail_id = pif.purchase_instruction_detail_id
		AND pif.purchase_instruction_future_id = ri.purchase_instruction_future_id
		AND ri.corn_future_id = cfwa.corn_future_id
		AND pid.customer_id = cfwa.customer_id
		AND piid.item_id = cfwa.item_id
	GROUP BY pif.application_future_period_id, cfwa.application_future_period_id, 
		pid.customer_id
		, piid.item_id
GO